package com.stylefeng.guns.rest.modular.wz.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.stylefeng.guns.rest.common.base.controller.ApiBaseController;
import com.stylefeng.guns.rest.common.persistence.dao.*;
import com.stylefeng.guns.rest.common.persistence.model.*;
import com.stylefeng.guns.rest.common.util.StringUtils;
import com.stylefeng.guns.rest.domain.ParamMap;
import com.stylefeng.guns.rest.enums.EnWzAdv;
import com.stylefeng.guns.rest.enums.EnWzUser;
import com.stylefeng.guns.rest.modular.wz.cache.CacheService;
import com.stylefeng.guns.rest.modular.wz.util.AliPushUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import sun.rmi.log.LogInputStream;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 异步调用
 */
@Service
public class AsyncService extends ApiBaseController {

    @Autowired
    private WzUserMapper userMapper;
    @Autowired
    private WzAdvMapper advMapper;
    @Autowired
    private WzAdvUserMapper advUserMapper;
    @Autowired
    private WzQuestionnaireMapper questionnaireMapper;
    @Autowired
    private WzUserCouponMapper userCouponMapper;
    @Autowired
    private PfLogMapper pfLogMapper;
    @Autowired
    private WzUserQuestionnaireMapper userQuestionnaireMapper;
    @Autowired
    private WzUserAdvRecordMapper advRecordMapper;
    @Autowired
    private AsyncSaveService asyncSaveService;
    @Autowired
    private WzUserFixedInfoMapper userFixedInfoMapper;
    @Autowired
    private CacheService cacheService;
    @Autowired
    private WzUserMessageMapper userMessageMapper;

    /**
     * 异步修改用户位置信息
     */
    @Async
    public void updateUserLocation(ParamMap paramMap,long userCode) throws Exception{
        List<WzUser> users = userMapper.selectList(new EntityWrapper<WzUser>().eq("user_code",userCode));
        String adCode = paramMap.getStrValid("adCode");
        String longitude = paramMap.getStrValid("longitude");
        String latitude = paramMap.getStrValid("latitude");
        userMapper.updateById(users.get(0));
        LOGGER.debug("异步修改用户位置信息");
    }

    /**
     * 异步发送邀请码通知
     */
    @Async
    public void invitationNotice(int type,String userCode,String title,String content,int messageType) throws Exception{
        if(type == 1){//ios
            AliPushUtil.pushNoticeToIOSAccount(userCode,title,content,messageType,null);
        }else if(type == 2){//安卓
            AliPushUtil.pushNoticeToAndroidAccount(userCode,title,content,messageType,null);
        }
        LOGGER.debug("异步发送邀请码通知");
    }

    /**
     * 异步发送用户发红包结果
     */
    @Async
    public void checkUserAdv(WzUserMessage userMessage,WzUser user,int type,JSONObject j) throws Exception{
        //消息
        userMessageMapper.insert(userMessage);
        if (user.getMobileType() == 1) {
            AliPushUtil.pushNoticeToIOSAccount(user.getUserCode().toString(), userMessage.getTitle(), userMessage.getContent(), type, j);
        } else if (user.getMobileType() == 2) {
            AliPushUtil.pushNoticeToAndroidAccount(user.getUserCode().toString(), userMessage.getTitle(), userMessage.getContent(),type, j);
        }
        user.setIsRead(0);
        userMapper.updateById(user);
    }


    /**
     * 修改用户信息手机型号、版本号
     */
//    @Async
    public void updateUser(ParamMap paramMap,WzUser user) {
        boolean r = false;
        if(!StringUtils.isBlankStr(paramMap.getStr("mobileType"))){
            if(user.getMobileType() != paramMap.getInt("mobileType")){
                user.setMobileType(paramMap.getInt("mobileType"));
                user.setUpdateTime(new Date());
                r = true;
            }
        }
//        if(!StringUtils.isBlankStr(paramMap.getStr("loginInfo"))){
//            if(!user.getLoginInfo().equals(paramMap.getStr("loginInfo"))){
//                user.setLoginInfo(paramMap.getStr("loginInfo"));
//                user.setUpdateTime(new Date());
//                r = true;
//            }
//        }
        if(!StringUtils.isBlankStr(paramMap.getStr("mobileVersion"))){
            if(!paramMap.getStr("mobileVersion").equals(user.getMobileVersion())){
                user.setMobileVersion(paramMap.getStr("mobileVersion"));
                user.setUpdateTime(new Date());
                r = true;
            }
        }
        if(!StringUtils.isBlankStr(paramMap.getStr("androidVersion"))){
            if(!paramMap.getStr("androidVersion").equals(user.getMobileVersion())){
                user.setMobileVersion(paramMap.getStr("androidVersion"));
                user.setUpdateTime(new Date());
                r = true;
            }
        }
//        if(!StringUtils.isBlankStr(paramMap.getStr("uuid"))){
//            if(StringUtils.isBlankStr(user.getUuid())){
//                user.setUuid(paramMap.getStr("uuid"));
//                user.setUpdateTime(new Date());
//                r = true;
//            }
//            if(!user.getUuid().equals(paramMap.getStr("uuid"))){
//                user.setUuid(paramMap.getStr("uuid"));
//                user.setUpdateTime(new Date());
//                r = true;
//            }
//        }
//        if(!StringUtils.isBlankStr(paramMap.getStr("uuid"))){
//            List<WzUserFixedInfo> userInfos = userFixedInfoMapper.selectList(new EntityWrapper<WzUserFixedInfo>().eq("user_code", user.getUserCode()));
//            if(StringUtils.isBlankStr(userInfos.get(0).getUuid())){
//                userInfos.get(0).setUuid(paramMap.getStr("uuid"));
//                userFixedInfoMapper.updateById(userInfos.get(0));
//            }
//        }
        if(r){
            userMapper.updateById(user);
        }
        LOGGER.debug("异步修改用户信息手机型号、版本号");
    }
}
